﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SmsGui
{
    class JsonSerializer
    {
        //public T Restore<T>(Hashtable ht) where T : new()
        //{
        //    return (T)null;
        //}

        public object Restore(Hashtable ht)
        {
            string typeName = ht.GetString("Type");

            Type type = Type.GetType(typeName);

            var constructor = type.GetConstructor(new Type[] { });

            object retval = constructor.Invoke(new object[] { });

            var properties = type.GetProperties();

            foreach (var property in properties)
            {
                var attributes = property.GetCustomAttributes(true);
                foreach (var attribute in attributes)
                {
                    var attr = attribute as JsonFieldAttribute;
                    if (attr != null)
                    {
                        property.SetValue(retval, ht[property.Name], null);
                    }
                }
            }

            return retval;
        }
    }
}
